package com.jhgsys.internal.hazard.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.entity.HazardTypeOperation;
import com.jhgsys.internal.hazard.service.IHazardTypeOperationService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;

/**
 * 隐患类型 Controller
 *
 * @author wanghw
 * @date 2024-08-29 15:10:21
 */
@Slf4j
@Validated
@Controller
public class HazardTypeOperationController extends BaseController {

    @Autowired
    private IHazardTypeOperationService hazardTypeOperationService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardTypeOperation")
    @RequiresPermissions("hazardTypeOperation:view")
    public String hazardTypeOperationIndex(){
        return FebsUtil.view("hazard/hazardTypeOperation/hazardTypeOperationList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardTypeOperation/add")
    @RequiresPermissions("hazardTypeOperation:add")
    public String hazardTypeOperationAdd(){
        return FebsUtil.view("hazard/hazardTypeOperation/hazardTypeOperationEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardTypeOperation/update/{id}")
    @RequiresPermissions("hazardTypeOperation:edit")
    public String hazardTypeOperationUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("hazardTypeOperation", hazardTypeOperationService.findHazardTypeOperationById(id));
        return FebsUtil.view("hazard/hazardTypeOperation/hazardTypeOperationEdit");
    }

    /**
     * 查询（所有）
     * @author wanghw
     * @date 2024-08-29 15:10:21
     */
    @GetMapping("hazardTypeOperation")
    @ResponseBody
    @RequiresPermissions("hazardTypeOperation:view")
    public FebsResponse getAllHazardTypeOperations(HazardTypeOperation hazardTypeOperation) {
        return new FebsResponse().success().data(hazardTypeOperationService.findHazardTypeOperationList(hazardTypeOperation));
    }

    /**
     * 查询（分页）
     * @author wanghw
     * @date 2024-08-29 15:10:21
     */
    @GetMapping("hazardTypeOperation/list")
    @ResponseBody
    @RequiresPermissions("hazardTypeOperation:view")
    public FebsResponse hazardTypeOperationList(QueryRequest request, HazardTypeOperation hazardTypeOperation) {
        Map<String, Object> dataTable = getDataTable(this.hazardTypeOperationService.findHazardTypeOperationPage(request, hazardTypeOperation));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 HazardTypeOperation
     * @author wanghw
     * @date 2024-08-29 15:10:21
     */
    @Log("新增HazardTypeOperation")
    @PostMapping("hazardTypeOperation")
    @ResponseBody
    @RequiresPermissions("hazardTypeOperation:add")
    public FebsResponse addHazardTypeOperation(@RequestBody  HazardTypeOperation hazardTypeOperation) throws FebsException {
        checkParam(hazardTypeOperation);
        try {
            List<HazardTypeOperation> queryData =  this.hazardTypeOperationService.list(new LambdaQueryWrapper<HazardTypeOperation>()
                    .eq(HazardTypeOperation::getTypeName,hazardTypeOperation.getTypeName())
                    .eq(HazardTypeOperation::getDeletedFlg,FebsConstant.VALID));
            if(!CollectionUtils.isEmpty(queryData)){
                return new FebsResponse().fail().message("该隐患类型名称已存在!");
            }
            hazardTypeOperation.setCreatedBy(getCurrentUser().getUserName());
            this.hazardTypeOperationService.createHazardTypeOperation(hazardTypeOperation);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增HazardTypeOperation失败";
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 HazardTypeOperation
     * @author wanghw
     * @date 2024-08-29 15:10:21
     */
    @Log("逻辑删除HazardTypeOperation")
    @GetMapping("hazardTypeOperation/delete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardTypeOperation:delete")
    public FebsResponse deleteHazardTypeOperation(@PathVariable("id") String hazardTypeOperationIds) throws FebsException {
        try {
            HazardTypeOperation hazardTypeOperation = new HazardTypeOperation();
            hazardTypeOperation.setDeletedBy(getCurrentUser().getUserName());
            hazardTypeOperation.setDeletedTime(new Date());
            hazardTypeOperation.setDeletedFlg(FebsConstant.INVALID);
            this.hazardTypeOperationService.deleteHazardTypeOperation(hazardTypeOperation,hazardTypeOperationIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardTypeOperation失败";
            log.error(message, e);
            return failure(e,message);
        }
    }



    /**
    * 物理删除 HazardTypeOperation
    * @author wanghw
    * @date 2024-08-29 15:10:21
    */
    @Log("物理删除HazardTypeOperation")
    @GetMapping("hazardTypeOperation/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardTypeOperation:delete")
    public FebsResponse physicallyDeleteHazardTypeOperation(@PathVariable("id") String hazardTypeOperationIds) throws FebsException {
        try {
            this.hazardTypeOperationService.physicallyDeleteHazardTypeOperation(hazardTypeOperationIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardTypeOperation失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 HazardTypeOperation
     * @author wanghw
     * @date 2024-08-29 15:10:21
     */
    @Log("修改HazardTypeOperation")
    @PostMapping("hazardTypeOperation/update")
    @ResponseBody
    @RequiresPermissions("hazardTypeOperation:edit")
    public FebsResponse updateHazardTypeOperation(@RequestBody HazardTypeOperation hazardTypeOperation) throws FebsException {
        if(StringUtils.isBlank(hazardTypeOperation.getTypeOperationId())){
            throw new FebsException("隐患类型ID不能为空");
        }
        checkParam(hazardTypeOperation);
        try {
            List<HazardTypeOperation> queryData =  this.hazardTypeOperationService.list(new LambdaQueryWrapper<HazardTypeOperation>()
                    .eq(HazardTypeOperation::getTypeName,hazardTypeOperation.getTypeName())
                    .eq(HazardTypeOperation::getDeletedFlg,FebsConstant.VALID));
            if(!CollectionUtils.isEmpty(queryData) && !queryData.get(0).getTypeOperationId().equals(hazardTypeOperation.getTypeOperationId())){
                return new FebsResponse().fail().message("该隐患类型名称已存在!");
            }
            hazardTypeOperation.setUpdatedBy(getCurrentUser().getUserName());
            this.hazardTypeOperationService.updateHazardTypeOperation(hazardTypeOperation);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改HazardTypeOperation失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 更改状态
     * @author wanghw
     * @date 2024-08-29 15:10:21
     */
    @Log("更改状态HazardTypeOperation")
    @PostMapping("hazardTypeOperation/updateStatus")
    @ResponseBody
    @RequiresPermissions("hazardTypeOperation:edit")
    public FebsResponse updateStatusHazardTypeOperation(@RequestBody HazardTypeOperation hazardTypeOperation) throws FebsException {
        if(StringUtils.isBlank(hazardTypeOperation.getTypeOperationId())){
            throw new FebsException("隐患类型ID不能为空");
        }
        if(StringUtils.isBlank(hazardTypeOperation.getStatus())){
            throw new FebsException("隐患类型状态不能为空");
        }
        try {
            hazardTypeOperation.setUpdatedBy(getCurrentUser().getUserName());
            hazardTypeOperation.setUpdatedTime(new Date());
            this.hazardTypeOperationService.updateHazardTypeOperation(hazardTypeOperation);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardTypeOperation失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    private void checkParam(HazardTypeOperation hazardTypeOperation) throws FebsException {
        if(StringUtils.isBlank(hazardTypeOperation.getTypeName())){
            throw new FebsException("隐患类型名称不能为空");
        }
        if(StringUtils.isBlank(hazardTypeOperation.getMajorAccidentFlg())){
            throw new FebsException("事故类型不能为空");
        }
        if(ObjectUtils.isEmpty(hazardTypeOperation.getSort())){
            throw new FebsException("隐患类型排序不能为空");
        }
        if(StringUtils.isBlank(hazardTypeOperation.getStatus())){
            throw new FebsException("隐患类型状态不能为空");
        }
    }

}
